<?php
/**
 * @package		简易CMS
 * @subpackage	文章等的通用管理操作模块
 * @version		$Id: admincp_moderate.php 23 2012-06-11 18:04:03Z htmambo@gmail.com $
 * @author		Hoping
 * @copyright	Copyright (C) 2010 Hoping Software Studio.
 */


if(!defined('IN_JYCMS') || !defined('IN_ADMINCP')) {
		exit('Access Denied');
}

cpheader();

$ignore = $_G['gp_ignore'];
$filter = $_G['gp_filter'];
$modfid = $_G['gp_modfid'];
$modsubmit = $_G['gp_modsubmit'];
$moderate = $_G['gp_moderate'];
$pm = $_G['gp_pm'];

if(empty($operation)) {
	$operation = 'article';
}
	if(isset($filter) && $filter == 'ignore') {
		$displayorder = -3;
		$filteroptions = '<option value="normal">'.$lang['moderate_none'].'</option><option value="ignore" selected>'.$lang['moderate_ignore'].'</option>';
	} else {
		$displayorder = -2;
		$filter = 'normal';
		$filteroptions = '<option value="normal" selected>'.$lang['moderate_none'].'</option><option value="ignore">'.$lang['moderate_ignore'].'</option>';
	}

	require_once libfile('function/misc');
	$modreasonoptions = '<option value="">'.$lang['none'].'</option><option value="">--------</option>'.modreasonselect(1);

function callback_js($id) {
	$js = "<script type='text/javascript'>mod_remove_row('$id');</script>";
	return $js;
}
if($operation == 'articles') {
	if(!submitcheck('modsubmit') && !$_G['gp_fast']) {
		shownav('portal', $lang['moderate_articles']);
		showsubmenu('nav_moderate_posts', array(
			array('nav_moderate_articles', 'moderate&operation=articles', 1),
			array('nav_moderate_articlecomments', 'moderate&operation=articlecomments', 0),
		));
		$select[$_G['gp_tpp']] = $_G['gp_tpp'] ? "selected='selected'" : '';
		$tpp_options = "<option value='20' $select[20]>$lang[perpage_20]</option><option value='50' $select[50]>$lang[perpage_50]</option><option value='100' $select[100]>$lang[perpage_100]</option>";
		$tpp = !empty($_G['gp_tpp']) ? $_G['gp_tpp'] : '20';
		$start_limit = ($page - 1) * $ppp;
		$dateline = $_G['gp_dateline'] ? $_G['gp_dateline'] : '604800';
		$dateline_options = '';
		foreach(array('all', '604800', '2592000', '7776000') as $v) {
			$selected = '';
			if($dateline == $v) {
				$selected = "selected='selected'";
			}
			$dateline_options .= "<option value=\"$v\" $selected>".cplang("dateline_$v");
		}
		$cat_select = '<option value="">'.$lang['all'].'</option>';
		$query = DB::query("SELECT catid, catname FROM #__common_category");
		while($cat = DB::fetch($query)) {
			$selected = '';
			if($cat['catid'] == $_G['gp_catid']) {
				$selected = 'selected="selected"';
			}
			$cat_select .= "<option value=\"$cat[catid]\" $selected>$cat[catname]</option>";
		}
		$article_status = 1;
		if($_G['gp_filter'] == 'ignore') {
			$article_status = 2;
		}
		showformheader("moderate&operation=articles");
		showtableheader('search');
		showtablerow('', array(''), array("<select name=\"filter\" style=\"margin: 0px;\">$filteroptions</select>
		$lang[moderate_article_category]: <select name=\"catid\">$cat_select</select>
		$lang[moderate_dateline]: <select name=\"dateline\" style=\"margin: 0px;\">$dateline_options</select>
		$lang[username]: <input size=\"15\" name=\"username\" type=\"text\" value=\"$_G[gp_username]\" />
		<select name=\"tpp\" style=\"margin: 0px;\">$tpp_options</select>
		<input class=\"btn\" type=\"submit\" value=\"$lang[search]\" />"));
		showtablefooter();

		$pagetmp = $page;
		$sqlwhere = "a.status='$article_status'";
		if(!empty($_G['gp_catid'])) {
			$sqlwhere .= " AND a.catid='{$_G['gp_catid']}'";
		}
		if(!empty($_G['gp_username'])) {
			$sqlwhere .= " AND a.username='{$_G['gp_username']}'";
		}
		if($dateline != 'all') {
			$sqlwhere .= " AND a.dateline>'".(TIMESTAMP - $dateline)."'";
		}

		$modcount = DB::result_first("SELECT COUNT(*)
			FROM #__portal_article_title a
			WHERE $sqlwhere");
		do {
			$start_limit = ($pagetmp - 1) * $tpp;
			$query = DB::query("SELECT a.aid, a.catid, a.uid, a.username, a.title, a.summary, a.dateline, cat.catname
				FROM #__portal_article_title a
				LEFT JOIN #__common_category cat ON cat.catid=a.catid
				WHERE $sqlwhere
				ORDER BY a.dateline DESC
				LIMIT $start_limit, $tpp");
				$pagetmp = $pagetmp - 1;
		} while($pagetmp > 0 && DB::num_rows($query) == 0);
		$page = $pagetmp + 1;
		$multipage = multi($modcount, $tpp, $page, ADMINSCRIPT."?action=moderate&operation=articles&filter=$filter&catid={$_G['gp_catid']}&dateline={$_G['gp_dateline']}&username={$_G['gp_username']}&keyword={$_G['gp_keyword']}&tpp=$tpp");


		echo '<p class="margintop marginbot"><a href="javascript:;" onclick="expandall();">'.cplang('moderate_all_expand').'</a> <a href="javascript:;" onclick="foldall();">'.cplang('moderate_all_fold').'</a></p>';


		showtableheader();
		require_once libfile('class/censor');
		$censor = & cmsCensor::instance();
		$censor->highlight = '#FF0000';
		require_once libfile('function/misc');
		while($article = DB::fetch($query)) {
			$article['dateline'] = dgmdate($article['dateline']);
			$censor->check($article['title']);
			$censor->check($article['summary']);
			$article_censor_words = $censor->words_found;
			if(count($article_censor_words) > 3) {
				$article_censor_words = array_slice($article_censor_words, 0, 3);
			}
			$article['censorwords'] = implode(', ', $article_censor_words);
			$article['modarticlekey'] = modauthkey($article['aid']);

			if(count($article_censor_words)) {
				$article_censor_text = "<span style=\"color: red;\">({$article['censorwords']})</span>";
			} else {
				$article_censor_text = '';
			}
			showtagheader('tbody', '', true, 'hover');
			showtablerow("id=\"mod_$article[aid]_row1\"", array("id=\"mod_$article[aid]_row1_op\" rowspan=\"3\" class=\"rowform threadopt\" style=\"width:80px;\"", '', 'width="120"', 'width="55"'), array(
				"<ul class=\"nofloat\"><li><input class=\"radio\" type=\"radio\" name=\"moderate[$article[aid]]\" id=\"mod_$article[aid]_1\" value=\"validate\" onclick=\"mod_setbg($article[aid], 'validate');\"><label for=\"mod_$article[aid]_1\">$lang[validate]</label></li><li><input class=\"radio\" type=\"radio\" name=\"moderate[$article[aid]]\" id=\"mod_$article[aid]_2\" value=\"delete\" onclick=\"mod_setbg($article[aid], 'delete');\"><label for=\"mod_$article[aid]_2\">$lang[delete]</label></li><li><input class=\"radio\" type=\"radio\" name=\"moderate[$article[aid]]\" id=\"mod_$article[aid]_3\" value=\"ignore\" onclick=\"mod_setbg($article[aid], 'ignore');\"><label for=\"mod_$article[aid]_3\">$lang[ignore]</label></li></ul>",
				"<h3><a href=\"javascript:;\" onclick=\"display_toggle({$article[aid]});\">$article[title] $article_censor_text</a></h3>",
				"<p><a target=\"_blank\" href=\"".ADMINSCRIPT."?action=members&operation=search&uid=$article[uid]&submit=yes\">$article[username]</a></p> <p>$article[dateline]</p>",
				"<a target=\"_blank\" href=\"index.php?task=view&aid=$article[aid]&modarticlekey=$article[modarticlekey]\">$lang[view]</a>&nbsp;<a href=\"index.php?option=portalcp&task=article&op=edit&aid=$article[aid]&modarticlekey=$article[modarticlekey]\" target=\"_blank\">$lang[edit]</a>",
			));



			showtablerow("id=\"mod_$article[aid]_row2\"", 'colspan="4" style="padding: 10px; line-height: 180%;"', '<div style="overflow: auto; overflow-x: hidden; max-height:120px; height:auto !important; height:100px; word-break: break-all;">'.$article['summary'].'</div>');



			showtablerow("id=\"mod_$article[aid]_row3\"", 'class="threadopt threadtitle" colspan="4"', "<a href=\"?action=moderate&operation=articles&fast=1&aid=$article[aid]&moderate[$article[aid]]=validate&page=$page&frame=no\" target=\"fasthandle\">$lang[validate]</a> | <a href=\"?action=moderate&operation=articles&fast=1&aid=$article[aid]&moderate[$article[aid]]=delete&page=$page&frame=no\" target=\"fasthandle\">$lang[delete]</a> | <a href=\"?action=moderate&operation=articles&fast=1&aid=$article[aid]&moderate[$article[aid]]=ignore&page=$page&frame=no\" target=\"fasthandle\">$lang[ignore]</a>");
			showtagfooter('tbody');
		}

		showsubmit('modsubmit', 'submit', '', '<a href="#all" onclick="mod_setbg_all(\'validate\')">'.cplang('moderate_all_validate').'</a> &nbsp;<a href="#all" onclick="mod_setbg_all(\'delete\')">'.cplang('moderate_all_delete').'</a> &nbsp;<a href="#all" onclick="mod_setbg_all(\'ignore\')">'.cplang('moderate_all_ignore').'</a> &nbsp;<a href="#all" onclick="mod_cancel_all();">'.cplang('moderate_all_cancel').'</a>', $multipage, false);
		showtablefooter();
		showformfooter();
	} else {
		$moderation = array('validate' => array(), 'delete' => array(), 'ignore' => array());
		$validates = $deletes = $ignores = 0;
		if(is_array($moderate)) {
			foreach((array)$moderate as $aid => $act) {
				$moderation[$act][] = $aid;
			}
		}

		if($validate_aids = dimplode($moderation['validate'])) {
			DB::update('portal_article_title', array('status' => '0'), "aid IN ($validate_aids)");
			$validates = DB::affected_rows();
		}
		if(!empty($moderation['delete'])) {
			require_once libfile('function/delete');
			$articles = deletearticle($moderation['delete']);
			$deletes = count($articles);
		}
		if($ignore_aids = dimplode($moderation['ignore'])) {
			DB::update('portal_article_title', array('status' => '2'), "aid IN ($ignore_aids)");
			$ignores = DB::affected_rows();
		}
		if($_G['gp_fast']) {
			echo callback_js($_G['gp_aid']);
			exit;
		} else {
			cpmsg('moderate_articles_succeed', "action=moderate&operation=articles&page=$page&filter=$filter&dateline={$_G['gp_dateline']}&username={$_G['gp_username']}&keyword={$_G['gp_keyword']}&idtype={$_G['gp_idtype']}&tpp={$_G['gp_tpp']}", 'succeed', array('validates' => $validates, 'ignores' => $ignores, 'deletes' => $deletes));
		}
	}
} elseif($operation == 'articlecomments') {
	if(!submitcheck('modsubmit') && !$_G['gp_fast']) {
		shownav('portal', $lang['moderate_articlecomments']);
		showsubmenu('nav_moderate_articlecomments', array(
			array('nav_moderate_articles', 'moderate&operation=articles', 0),
			array('nav_moderate_articlecomments', 'moderate&operation=articlecomments', 1),
		));

		$select[$_G['gp_tpp']] = $_G['gp_tpp'] ? "selected='selected'" : '';
		$tpp_options = "<option value='20' $select[20]>$lang[perpage_20]</option><option value='50' $select[50]>$lang[perpage_50]</option><option value='100' $select[100]>$lang[perpage_100]</option>";
		$tpp = !empty($_G['gp_tpp']) ? $_G['gp_tpp'] : '20';
		$start_limit = ($page - 1) * $ppp;
		$dateline = $_G['gp_dateline'] ? $_G['gp_dateline'] : '604800';
		$dateline_options = '';
		foreach(array('all', '604800', '2592000', '7776000') as $v) {
			$selected = '';
			if($dateline == $v) {
				$selected = "selected='selected'";
			}
			$dateline_options .= "<option value=\"$v\" $selected>".cplang("dateline_$v");
		}
		$cat_select = '<option value="">'.$lang['all'].'</option>';
		$query = DB::query("SELECT catid, catname FROM #__common_category");
		while($cat = DB::fetch($query)) {
			$selected = '';
			if($cat['catid'] == $_G['gp_catid']) {
				$selected = 'selected="selected"';
			}
			$cat_select .= "<option value=\"$cat[catid]\" $selected>$cat[catname]</option>";
		}
		$articlecomment_status = 1;
		if($_G['gp_filter'] == 'ignore') {
			$articlecomment_status = 2;
		}
		showformheader("moderate&operation=articlecomments");
		showtableheader('search');
		showtablerow('', array(''), array("<select name=\"filter\" style=\"margin: 0px;\">$filteroptions</select>
		$lang[moderate_article_category]: <select name=\"catid\">$cat_select</select>
		$lang[moderate_dateline]: <select name=\"dateline\" style=\"margin: 0px;\">$dateline_options</select>
		$lang[username]: <input size=\"15\" name=\"username\" type=\"text\" value=\"$_G[gp_username]\" />
		$lang[moderate_content_keyword]: <input size=\"15\" name=\"keyword\" type=\"text\" value=\"$_G[gp_keyword]\" />
		<select name=\"tpp\" style=\"margin: 0px;\">$tpp_options</select>
		<input class=\"btn\" type=\"submit\" value=\"$lang[search]\" />"));
		showtablefooter();

		$pagetmp = $page;
		$sqlwhere = "c.status='$articlecomment_status'";
		if(!empty($_G['gp_catid'])) {
			$sqlwhere .= " AND a.catid='{$_G['gp_catid']}'";
		}
		if(!empty($_G['gp_username'])) {
			$sqlwhere .= " AND c.username='{$_G['gp_username']}'";
		}
		if($dateline != 'all') {
			$sqlwhere .= " AND c.dateline>'".(TIMESTAMP - $dateline)."'";
		}
		if(!empty($_G['gp_keyword'])) {
			$sqlwhere .= " AND c.message LIKE '%{$_G['gp_keyword']}%'";
		}
		$modcount = DB::result_first("SELECT COUNT(*)
			FROM #__common_comment c
			LEFT JOIN #__portal_article_title a ON a.aid=c.aid
			WHERE $sqlwhere");
		do {
			$start_limit = ($pagetmp - 1) * $tpp;
			$query = DB::query("SELECT c.cid, c.uid, c.username, c.aid, c.postip, c.dateline, c.message, a.title, cat.catname
				FROM #__common_comment c
				LEFT JOIN #__portal_article_title a ON a.aid=c.aid
				LEFT JOIN #__common_category cat ON cat.catid=a.catid
				WHERE $sqlwhere
				ORDER BY c.dateline DESC
				LIMIT $start_limit, $tpp");
				$pagetmp = $pagetmp - 1;
		} while($pagetmp > 0 && DB::num_rows($query) == 0);
		$page = $pagetmp + 1;
		$multipage = multi($modcount, $tpp, $page, ADMINSCRIPT."?action=moderate&operation=articlecomments&filter=$filter&modfid=$modfid&ppp=$tpp");


		echo '<p class="margintop marginbot"><a href="javascript:;" onclick="expandall();">'.cplang('moderate_all_expand').'</a> <a href="javascript:;" onclick="foldall();">'.cplang('moderate_all_fold').'</a></p>';


		showtableheader();
		require_once libfile('class/censor');
		$censor = & cmsCensor::instance();
		$censor->highlight = '#FF0000';
		require_once libfile('function/misc');
		while($articlecomment = DB::fetch($query)) {
			$articlecomment['dateline'] = dgmdate($articlecomment['dateline']);
			$censor->check($articlecomment['title']);
			$censor->check($articlecomment['message']);
			$articlecomment_censor_words = $censor->words_found;
			if(count($articlecomment_censor_words) > 3) {
				$articlecomment_censor_words = array_slice($articlecomment_censor_words, 0, 3);
			}
			$articlecomment['censorwords'] = implode(', ', $articlecomment_censor_words);
			$articlecomment['modarticlekey'] = modauthkey($articlecomment['aid']);
			$articlecomment['modarticlecommentkey'] = modauthkey($articlecomment['cid']);

			if(count($articlecomment_censor_words)) {
				$articlecomment_censor_text = "<span style=\"color: red;\">({$articlecomment['censorwords']})</span>";
			} else {
				$articlecomment_censor_text = '';
			}
			showtagheader('tbody', '', true, 'hover');
			showtablerow("id=\"mod_$articlecomment[cid]_row1\"", array("id=\"mod_$articlecomment[cid]_row1_op\" rowspan=\"3\" class=\"rowform threadopt\" style=\"width:80px;\"", '', 'width="120"', 'width="55"'), array(
				"<ul class=\"nofloat\"><li><input class=\"radio\" type=\"radio\" name=\"moderate[$articlecomment[cid]]\" id=\"mod_$articlecomment[cid]_1\" value=\"validate\" onclick=\"mod_setbg($articlecomment[cid], 'validate');\"><label for=\"mod_$articlecomment[cid]_1\">$lang[validate]</label></li><li><input class=\"radio\" type=\"radio\" name=\"moderate[$articlecomment[cid]]\" id=\"mod_$articlecomment[cid]_2\" value=\"delete\" onclick=\"mod_setbg($articlecomment[cid], 'delete');\"><label for=\"mod_$articlecomment[cid]_2\">$lang[delete]</label></li><li><input class=\"radio\" type=\"radio\" name=\"moderate[$articlecomment[cid]]\" id=\"mod_$articlecomment[cid]_3\" value=\"ignore\" onclick=\"mod_setbg($articlecomment[cid], 'ignore');\"><label for=\"mod_$articlecomment[cid]_3\">$lang[ignore]</label></li></ul>",
				"<h3><a href=\"javascript:;\" onclick=\"display_toggle({$articlecomment[cid]});\">$articlecomment[title] $articlecomment_censor_text</a></h3>",
				"<p><a target=\"_blank\" href=\"".ADMINSCRIPT."?action=members&operation=search&uid=$articlecomment[uid]&submit=yes\">$articlecomment[username]</a></p> <p>$articlecomment[dateline]</p>",
				"<a target=\"_blank\" href=\"index.php?task=view&aid=$articlecomment[aid]&modarticlekey=$articlecomment[modarticlekey]#comment_anchor_{$articlecomment[cid]}\">$lang[view]</a>&nbsp;<a href=\"index.php?option=comment&task=manage&op=edit&cid=$articlecomment[cid]&modarticlecommentkey=$articlecomment[modarticlecommentkey]\" target=\"_blank\">$lang[edit]</a>",
			));



			showtablerow("id=\"mod_$articlecomment[cid]_row2\"", 'colspan="4" style="padding: 10px; line-height: 180%;"', '<div style="overflow: auto; overflow-x: hidden; max-height:120px; height:auto !important; height:100px; word-break: break-all;">'.$articlecomment['message'].'</div>');



			showtablerow("id=\"mod_$articlecomment[cid]_row3\"", 'class="threadopt threadtitle" colspan="4"', "<a href=\"?action=moderate&operation=articlecomments&fast=1&cid=$articlecomment[cid]&moderate[$articlecomment[cid]]=validate&page=$page&frame=no\" target=\"fasthandle\">$lang[validate]</a> | <a href=\"?action=moderate&operation=articlecomments&fast=1&cid=$articlecomment[cid]&moderate[$articlecomment[cid]]=delete&page=$page&frame=no\" target=\"fasthandle\">$lang[delete]</a> | <a href=\"?action=moderate&operation=articlecomments&fast=1&cid=$articlecomment[cid]&moderate[$articlecomment[cid]]=ignore&page=$page&frame=no\" target=\"fasthandle\">$lang[ignore]</a>");
			showtagfooter('tbody');
		}

		showsubmit('modsubmit', 'submit', '', '<a href="#all" onclick="mod_setbg_all(\'validate\')">'.cplang('moderate_all_validate').'</a> &nbsp;<a href="#all" onclick="mod_setbg_all(\'delete\')">'.cplang('moderate_all_delete').'</a> &nbsp;<a href="#all" onclick="mod_setbg_all(\'ignore\')">'.cplang('moderate_all_ignore').'</a> &nbsp;<a href="#all" onclick="mod_cancel_all();">'.cplang('moderate_all_cancel').'</a>', $multipage, false);
		showtablefooter();
		showformfooter();
	} else {
		$moderation = array('validate' => array(), 'delete' => array(), 'ignore' => array());
		$validates = $deletes = $ignores = 0;
		if(is_array($moderate)) {
			foreach((array)$moderate as $cid => $act) {
				$moderation[$act][] = $cid;
			}
		}

		if($validate_cids = dimplode($moderation['validate'])) {
			DB::update('common_comment', array('status' => '0'), "cid IN ($validate_cids)");
			$validates = DB::affected_rows();
		}
		if($delete_cids = dimplode($moderation['delete'])) {
			DB::delete('common_comment', "cid IN ($delete_cids)");
			$deletes = DB::affected_rows();
		}
		if($ignore_cids = dimplode($moderation['ignore'])) {
			DB::update('common_comment', array('status' => '2'), "cid IN ($ignore_cids)");
			$ignores = DB::affected_rows();
		}

		if($_G['gp_fast']) {
			echo callback_js($_G['gp_cid']);
			exit;
		} else {
			cpmsg('moderate_articlecomments_succeed', "action=moderate&operation=articlecomments&page=$page&filter=$filter&dateline={$_G['gp_dateline']}&username={$_G['gp_username']}&keyword={$_G['gp_keyword']}&catid={$_G['gp_catid']}&tpp={$_G['gp_tpp']}", 'succeed', array('validates' => $validates, 'ignores' => $ignores, 'deletes' => $deletes));
		}
	}
}
echo '<iframe name="fasthandle" style="display: none;"></iframe>';
